package com.wisnews.util;

import org.apache.shiro.crypto.hash.SimpleHash;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.UUID;

/**
 * @Description：shiro 加密工具类
 * @Author: yuyd
 * @Email: yyd8358@foxmail.com
 * @Date: 2020/12/15 10:47
 * @Version 1.0
 */
public class ShiroUtil {

    private static final byte[] KEY = "YUANDONG".getBytes();
    private static final String ALGORITHM_DES = "DES";
    private static final String ALGORITHM_MD5 = "MD5";

    /**
     * 生成 16位的随机盐值
     */
    public static String createSalt(){

        String str = UUID.randomUUID().toString().replaceAll("-", "");

        return str.substring(0,16);
    }

    /**
     * 加盐加密
     * @param str    原始密码
     * @param salt 盐值
     */
    public static String encryption(Object str, String salt){
        return new SimpleHash("MD5", str, salt, Constants.SYS_CODE.PSWD_DEGREE).toString();
    }

    public static String simpleEncrypt(Object str){
        return new SimpleHash("MD5", str).toString();
    }

    /**
     * 加密
     *
     * @Method encode
     * @Return String
     * @param input
     * @Date 2021年1月9日 上午10:38:03
     * @Version v1.0.0
     */
    public static String encode(String input) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, ALGORITHM_DES));
            return byte2hex(cipher.doFinal(input.getBytes()));
        } catch (Exception e) {
            return input;
        }
    }

    /**
     * 解密
     *
     * @Method decode
     * @Return String
     * @param input
     * @Date 2021年1月9日 上午10:37:48
     * @Version v1.0.0
     */
    public static String decode(String input) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, ALGORITHM_DES));
            return new String(cipher.doFinal(hex2byte(input)));
        } catch (Exception e) {
            return input;
        }
    }

    /**
     * 字符串MD5
     *
     * @Method md5
     * @Return String
     * @param input
     * @throws Exception
     * @Date 2021年1月9日 上午10:37:31
     * @Version v1.0.0
     */
    public static String md5(String input) throws Exception {
        try {
            MessageDigest alg = MessageDigest.getInstance(ALGORITHM_MD5);
            alg.update(input.getBytes());
            return new String(alg.digest());
        } catch (Exception e) {
            return input;
        }
    }

    /**
     * 字节码转16进制字符串,使用次数较少。在循环中拼接字符串。优化可读性。请忽略性能问题
     *
     * @Method byte2hex
     * @Return String
     * @param bits
     * @Date 2021年1月9日 上午10:36:23
     * @Version v1.0.0
     */
    private static String byte2hex(byte[] bits) {
        String hs = "", stmp = "";
        for (byte element : bits) {
            stmp = Integer.toHexString(element & 0XFF);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }

    /**
     * 16进制码转字节码
     *
     * @Method hex2byte
     * @Return byte[]
     * @param hex
     * @Date 2021年1月9日 上午10:35:54
     * @Version v1.0.0
     */
    private static byte[] hex2byte(String hex) {
        if (hex.length() % 2 != 0) {
            throw new IllegalArgumentException();
        }
        char[] arr = hex.toCharArray();
        byte[] bits = new byte[hex.length() / 2];
        for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
            String swap = "" + arr[i++] + arr[i];
            int byteint = Integer.parseInt(swap, 16) & 0xFF;
            bits[j] = new Integer(byteint).byteValue();
        }
        return bits;
    }

    public static void main(String[] args) {
        System.out.println(decode("6B8F13CB6E987659357A745BDFCDCB19"));

    }

}
